package com.app.minutes.widget;

import com.nerc.minutes.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TabSwicherControl extends LinearLayout {
	private static final String tag = "TabSwitcher";
	private Context context;
	private String[] texts;
	private int arrayId;
	private int selectedPosition = 0;
	private int oldPosition = selectedPosition;
	private int[] background = { R.drawable.special_desc_y,
			R.drawable.special_desc_n, R.drawable.special_course_y,
			R.drawable.special_course_n };
	private TextView[] tvs;

	private int dataValue = R.styleable.custom_arrayId;

	public TabSwicherControl(Context context) {
		super(context);
	}

	public TabSwicherControl(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		TypedArray a = context
				.obtainStyledAttributes(attrs, R.styleable.custom);
		arrayId = a.getResourceId(dataValue, 0);
		selectedPosition = a.getInt(R.styleable.custom_selectedPosition, 0);
		a.recycle();
	}

	private void init() {
		context = getContext();
		setOrientation(HORIZONTAL);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);
		params.weight = 1;
		setLayoutParams(params);
	}

	@SuppressLint("ResourceAsColor")
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		Log.i(tag, "--------------onFinishInflate---------------------");
		if (arrayId != 0) {
			texts = getResources().getStringArray(arrayId);
		} else {
			texts = new String[] {};
		}

		tvs = new TextView[texts.length];

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);
		params.weight = 1;
		params.width = LinearLayout.LayoutParams.MATCH_PARENT;
		params.gravity = Gravity.CENTER_VERTICAL;
		for (int i = 0; i < 2; i++) {
			TextView child = new TextView(context);
			child.setTag(i);
			child.setTextSize(16);
			child.setTextColor(R.color.white);
			child.setGravity(Gravity.CENTER);
			tvs[i] = child;
			child.setOnClickListener(listener);
			this.addView(child, params);
		}
	}

	OnClickListener listener = new OnClickListener() {
		@SuppressLint("ResourceAsColor")
		public void onClick(View v) {
			selectedPosition = (Integer) v.getTag();
			if (selectedPosition != oldPosition) {
				if (selectedPosition == 0) {
					tvs[1].setBackgroundResource(background[3]);
					oldPosition = selectedPosition;
					((TextView) v).setBackgroundResource(background[0]);
					if (onItemClickLisener != null) {
						onItemClickLisener.onItemClickLisener(v,
								selectedPosition);
					}
				} else {
					tvs[0].setBackgroundResource(background[1]);
					oldPosition = selectedPosition;
					((TextView) v).setBackgroundResource(background[2]);
					if (onItemClickLisener != null) {
						onItemClickLisener.onItemClickLisener(v,
								selectedPosition);
					}
				}
			}

		}

	};

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		if (selectedPosition > texts.length - 1) {
			throw new IllegalArgumentException(
					"The selectedPosition can't be > texts.length.");
		}
		oldPosition = selectedPosition;
		for (int i = 0; i < texts.length; i++) {
			if (selectedPosition == i) {
				tvs[i].setBackgroundResource(background[0]);
			} else {
				tvs[i].setBackgroundResource(background[3]);
			}
		}
	}

	private OnItemClickLisener onItemClickLisener;

	public void setOnItemClickLisener(OnItemClickLisener onItemClickLisener) {
		this.onItemClickLisener = onItemClickLisener;
	}

	public interface OnItemClickLisener {
		void onItemClickLisener(View view, int position);
	}

	public void setTexts(String[] texts) {
		this.texts = texts;
	}

	public void setSelectedPosition(int selectedPosition) {
		this.selectedPosition = selectedPosition;
	}
}
